//
//  ST_LiveP2PHeaderView.swift
//  btvideo
//
//  Created by Jason on 2021/12/20.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import SnapKit
import FSPagerView
import CHIPageControl

class ST_LiveP2PHeaderView: UICollectionReusableView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(pagerView)
        pagerView.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(UIEdgeInsets(top: 4, left: 15, bottom: 0, right: 15))
        }
        
        addSubview(pageControl)
        pageControl.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(-5)
            make.left.right.equalToSuperview()
            make.height.equalTo(2)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var pageControl:CHIPageControlJaloro = {
        let l = CHIPageControlJaloro(frame: .zero)
        l.numberOfPages = 4
        l.radius = 4
        l.tintColor = UIColor(hex: 0xE8E8E8)
        l.currentPageTintColor = UIColor.white
        l.padding = 10
        return l
    }()
    
    fileprivate lazy var pagerView:FSPagerView = {
        let l = FSPagerView()
        l.dataSource = self
        l.delegate = self
        l.cornerRadius = 15
        l.automaticSlidingInterval = 5
        l.register(ST_TianPagerCollectionCell.self, forCellWithReuseIdentifier: "cell")
        return l
    }()
    
    var data_array = [ST_MsgBannerHeaderCellModel](){
        didSet{
            self.pagerView.reloadData()
            self.pagerView.automaticSlidingInterval = 5
            pageControl.numberOfPages = data_array.count
        }
    }
}


extension ST_LiveP2PHeaderView:FSPagerViewDataSource,FSPagerViewDelegate{
    
    func pagerView(_ pagerView: FSPagerView, willDisplay cell: FSPagerViewCell, forItemAt index: Int) {
        
        pageControl.progress = index.double
    }
    
    func numberOfItems(in pagerView: FSPagerView) -> Int {
        return data_array.count
    }
    
    
    func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)
        
        guard let iCell = cell as? ST_TianPagerCollectionCell else {
            return cell
        }
        
        iCell.bannerImageView.pin_setImage(from: data_array[safe: index]?.thumb.url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        return iCell
    }
    
    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        
        guard let item = data_array[safe: index] else{
            return
        }
        
        let vc = ST_BTWebViewController()
        
        vc.urlStr = item.url
        
        vc.titleStr = item.name
        
        self.parentViewController?.navigationController?.pushViewController(vc)
        
    }
    
}
